package getPortsList

import (
	"log"

	"go.bug.st/serial"
)

func InitSerialPort() (ports []string, err error) {
	ports, err = serial.GetPortsList()
	if err != nil {
		log.Fatal(err)
	}
	if len(ports) == 0 {
		log.Fatal("没有找到串口")
	}
	log.Printf("%#v", ports)
	for _, port := range ports {
		log.Printf("找到串口: %v\n", port)
	}
	return
}

type portList struct {
	ID    int    `json:"id"`
	ProNo string `json:"proNo"`
}

func GetSerialPortName() []portList {
	var (
		portListJson = make([]portList, 0, 200)
	)

	ports, _ := InitSerialPort()
	portListJson = nil
	for i := 0; i < len(ports); i++ {
		temPortL := portList{
			ID:    i,
			ProNo: ports[i],
		}
		portListJson = append(portListJson, temPortL)
	}

	return portListJson
}
